package com.guo.pet_store.admin.service;

import com.guo.pet_store.admin.dao.PetRepository;
import com.guo.pet_store.admin.dao.ShopCarRepository;
import com.guo.pet_store.admin.pojo.Pet;
import com.guo.pet_store.admin.pojo.PetQuery;
import com.guo.pet_store.admin.pojo.ShopCar;
import com.guo.pet_store.admin.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.awt.print.Pageable;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService{
    @Autowired
    ShopCarRepository shopCarRepository;
    @Autowired
    TypeService typeService;
    @Autowired
    UserService userService;
    @Autowired
    PetRepository petRepository;

    //给购物车添加商品
    @Override
    public Boolean addPet(Pet pet,ShopCar shopCar) {
       shopCarRepository.shopCarAddItem(pet.getId(), shopCar.getId());
       return true;
    }

    //根据购物车id 查询购物车内所有的商品
    @Override
    public List<Pet> findAllItems(Long CarId) {
        return petRepository.findAll(new Specification<Pet>() {
            @Override
            public Predicate toPredicate(Root<Pet> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                Join join = root.join("shopCar");
                return cb.equal(join.get("id"),CarId);
            }
        });
    }
//    根据购物车id 查询对应宠物的数量
    public List<Integer> findPetitems(Long CarId){
        return petRepository.getCarnumbers(CarId);
    }

    @Override
    public void deleteAItemFromCar(Long petId, Long carId) {
        shopCarRepository.deleteACarItem(petId,carId);
    }

    @Override
    public void connectToUser(Long userId) {
        shopCarRepository.connectToUser(userId);
    }

    @Override
    public ShopCar findShopCarByUser(User user) {
        return shopCarRepository.findShopCarByUser(user);
    }

    @Override
    public void clearCar(User user) {
        shopCarRepository.clearCar(user.getShopCar().getId());
        System.out.println("====================ShopServiceImpl 运行了");
    }
}
